package com.joolun.web.controller.activity;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joolun.activity.domain.ActivityTake;
import com.joolun.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.activity.domain.ActivityTakeInfo;
import com.joolun.activity.service.IActivityTakeInfoService;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.common.core.page.TableDataInfo;

/**
 * 活动参与码明细Controller
 *
 * @author www.joolun.com
 * @date 2022-11-22
 */
@RestController
@RequestMapping("/activity/takeInfo")
public class ActivityTakeInfoController extends BaseController
{
    @Autowired
    private IActivityTakeInfoService activityTakeInfoService;

    /**
     * 查询活动参与码明细列表
     */
    @PreAuthorize("@ss.hasPermi('activity:takeInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(ActivityTakeInfo activityTakeInfo)
    {
        startPage();
        List<ActivityTakeInfo> list = activityTakeInfoService.selectActivityTakeInfoList(activityTakeInfo);
        return getDataTable(list);
    }

    /**
     * 查询活动参与码每日合计绑定总数列表
     */
    @PreAuthorize("@ss.hasPermi('activity:take:list')")
    @GetMapping("/list/total")
    public TableDataInfo listTotal(ActivityTakeInfo activityTakeInfo)
    {
        startPage();
        QueryWrapper<ActivityTakeInfo> queryWrapper=new QueryWrapper<ActivityTakeInfo>();
        queryWrapper.select("DATE_FORMAT(create_time,'%Y-%m-%d') as create_times,IFNULL(count(id),0) as count ,gen_dept,gen_dept_id");
        if(SecurityUtils.getLoginUser().getUser().getDeptId()!=100L){
            queryWrapper.eq("gen_dept_id", SecurityUtils.getLoginUser().getUser().getDeptId());
        }
        queryWrapper.eq("status","2");
        queryWrapper.groupBy("create_times,gen_dept_id");
        queryWrapper.orderByDesc("create_times");
        List<ActivityTakeInfo> list = activityTakeInfoService.list(queryWrapper);
        return getDataTable(list);
    }

    /**
     * 导出活动参与码明细列表
     */
    @PreAuthorize("@ss.hasPermi('activity:takeInfo:export')")
    @Log(title = "活动参与码明细", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ActivityTakeInfo activityTakeInfo)
    {
        List<ActivityTakeInfo> list = activityTakeInfoService.selectActivityTakeInfoList(activityTakeInfo);
        ExcelUtil<ActivityTakeInfo> util = new ExcelUtil<ActivityTakeInfo>(ActivityTakeInfo.class);
        return util.exportExcel(list, "takeInfo");
    }

    /**
     * 获取活动参与码明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('activity:takeInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(activityTakeInfoService.selectActivityTakeInfoById(id));
    }

    /**
     * 新增活动参与码明细
     */
    @PreAuthorize("@ss.hasPermi('activity:takeInfo:add')")
    @Log(title = "活动参与码明细", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ActivityTakeInfo activityTakeInfo)
    {
        return toAjax(activityTakeInfoService.insertActivityTakeInfo(activityTakeInfo));
    }

    /**
     * 修改活动参与码明细
     */
    @PreAuthorize("@ss.hasPermi('activity:takeInfo:edit')")
    @Log(title = "活动参与码明细", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ActivityTakeInfo activityTakeInfo)
    {
        return toAjax(activityTakeInfoService.updateActivityTakeInfo(activityTakeInfo));
    }

    /**
     * 取消活动参与码明细
     */
    @PreAuthorize("@ss.hasPermi('activity:takeInfo:remove')")
    @Log(title = "活动参与码明细", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        //查询信息
        ActivityTakeInfo activityTakeInfo= activityTakeInfoService.getById(ids[0]);
        if(null==activityTakeInfo){
            throw new RuntimeException("查无此参与信息");
        }
        if(activityTakeInfo.getStatus().equals("1")){
            throw new RuntimeException("未绑定不能进行取消");
        }
        if(activityTakeInfo.getStatus().equals("3")){
            throw new RuntimeException("已中奖不能进行取消");
        }
        if(activityTakeInfo.getStatus().equals("2")){
            activityTakeInfo.setStatus("4");
            activityTakeInfoService.updateById(activityTakeInfo);
        }
        return AjaxResult.success();
    }

}
